package com.yiyi.credit.manage.abutment.provider;

import com.yiyi.credit.manage.model.entity.SubStation;
import com.yiyi.credit.manage.model.po.SubstationTradePO;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;


import java.util.Map;


public class SubStationSqlProvider {

    public String getSubStationPageList(Map<String, Object> paramMap) {
        return new SQL() {{
            SELECT("id,sub_station_no subStationNo,sub_station_name subStationName,status,charge_people chargePeople,contact_phone contactPhone,create_time createTime,create_people createPeople,update_time updateTime,update_people updatePeople");
            FROM("sub_station");
            WHERE(buildWhereSql(paramMap));
        }}.toString();
    }

    public String listTrades(SubstationTradePO tradePO) {
        return new SQL() {{
            SELECT("  i.id, i.investor_login_name investorLoginName, i.investment_start_time investmentTime, " +
                    " i.investment_amount investmentAmount, i.project_no projectNo, i.substation_id substationId,  " +
                    " i.sub_product_id subProductId, i.sub_product_name subProductName, i.red_packet_amount redPacket, " +
                    " i.actual_payment_amount realPayment, r.redemption_start_time redemptionTime,i.investment_period investmentPeriod, " +
                    " i.investment_period_unit investmentPeriodUnit,m.status mStatus,s.status sStatus,i.project_id projectId,i.project_type projectType ");
            FROM(" investment_record i left join redemption_record r on r.investment_no = i.investment_record_no  left join " +
                    "  multi_asset_project m on i.project_no=m.project_no  left join single_asset_project s  on  i.project_no=s.project_no ");
            StringBuilder builder = new StringBuilder(" 1=1 ");
            if (!StringUtils.isEmpty(tradePO.getInvestorLoginName())) {
                tradePO.setInvestorLoginName("%" + tradePO.getInvestorLoginName() + "%");
                builder.append(" and i.investor_login_name like #{investorLoginName} ");
            }

            if (!StringUtils.isEmpty(tradePO.getProjectNo())) {
                tradePO.setProjectNo("%" + tradePO.getProjectNo() + "%");
                builder.append(" and i.project_no like #{projectNo} ");
            }

            if (tradePO.getSubstationId() != null) {
                builder.append(" and i.substation_id = #{substationId} ");
            }

            if (tradePO.getSubstationProductId() != null) {
                builder.append(" and i.sub_product_id = #{substationProductId} ");
            }

            if (tradePO.getStartDate() != null) {
                builder.append(" and i.investment_start_time >= #{startDate} ");
            }

            if (tradePO.getEndDate() != null) {
                builder.append(" and i.investment_start_time < #{endDate} ");
            }

            WHERE(builder.toString());
        }}.toString();
    }

    private String buildWhereSql(Map<String, Object> paramMap) {

        SubStation subStation = (SubStation) paramMap.get("subStation");
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" 1=1 ");
        if (null != subStation.getSubStationNo()
                && StringUtils.isNotBlank(subStation.getSubStationNo())) {
            sqlBuilder.append(" AND sub_station_no = #{subStation.subStationNo}");
        }
        if (null != subStation.getSubStationName()
                && StringUtils.isNotBlank(subStation.getSubStationName())) {
            sqlBuilder.append(" AND sub_station_name = #{subStation.subStationName} ");
        }
        if (null != subStation.getStatus()
                && StringUtils.isNotBlank(String.valueOf(subStation.getStatus()))) {
            if (subStation.getStatus() != -1) {
                sqlBuilder.append(" AND status = #{subStation.status} ");
            }
        }

        return sqlBuilder.toString();

    }

}